using UnityEngine;
using System.Collections;

public abstract class Cat : DrawableGameObject 
{
    public GameObject meGo;
    protected GameObject playerGo;  

    protected PlayerState playerState;

    protected bool attackMode = false;
    public bool jumped;
	protected bool death = false;
	private bool lookAtLaser;
	private bool zonzo;
	
    public float xNeutralVel;
    private float minimalHeight = -8.5f;

    protected float xJumpForce;
    protected float yJumpForce;

    public float minAttackXDistanceForce = 0;
    public float maxAttackXDistanceForce = 0;

    public float minAttackYDistanceForce = 0;
    public float maxAttackYDistanceForce = 0;

    public float finalOfScreen = 12;
	
	private float timer;
	private float timeToJump;
	
    public override void Start() 
    {
		base.Start();
			
        meGo = gameObject;
		meGo.tag = Tags.ENEMY;
		playerGo = GameObject.FindGameObjectWithTag(Tags.PLAYER);

        xJumpForce = Random.Range(this.minAttackXDistanceForce, this.maxAttackXDistanceForce);
        yJumpForce = Random.Range(this.minAttackYDistanceForce, this.maxAttackYDistanceForce);
	}

    public virtual void Update() 
    {
        try
        {
            playerState = playerGo.GetComponent<PlayerMechanic>().getPlayerState();
        }
        catch { }

        if(!attackMode)
        {
            meGo.transform.Translate(new Vector3(xNeutralVel, 0, 0) * Time.deltaTime);
        }
        else
        {
            if(!jumped)
            {
                Jump();
            }
        }

        if(meGo.transform.position.x >= finalOfScreen)
        {
            attackMode = true;
        }

        if(transform.position.y < minimalHeight)
        {
            Destroy(gameObject);
        }
		
		if(playerState != PlayerState.LaserMode)
		{
			lookAtLaser = false;
		}
		
		if(jumped && lookAtLaser)
		{
			GameObject laser = GameObject.FindGameObjectWithTag(Tags.LASER);

            Quaternion newRotation = Quaternion.LookRotation(transform.position - laser.transform.position, Vector3.right);

            newRotation.x = 0.0f;
            newRotation.y = 0.0f;

            transform.rotation = newRotation;

            rigidbody.AddForce(-xJumpForce, yJumpForce, 0);
            lookAtLaser = false;
		}
	}

    public virtual void Jump()
    {
        jumped = true;

        if(playerState.Equals(PlayerState.LaserMode))
        {
        	lookAtLaser = true;
			zonzo = true;
            rigidbody.constraints -= RigidbodyConstraints.FreezeRotationZ;
		} 
	}
	
	
    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.tag == Tags.FLOOR && jumped)
        {
            RunAnimation("First", "Loop");

            transform.rotation = Quaternion.Euler(Vector3.zero);
            rigidbody.constraints = RigidbodyConstraints.FreezeRotation | RigidbodyConstraints.FreezePositionZ;
        }
    }
	
	void OnCollisionStay(Collision other)
	{
		if(other.gameObject.tag == Tags.FLOOR)
		{
			timer += Time.deltaTime;
		}
		
		if(other.gameObject.tag == Tags.FLOOR && jumped)
        {
			meGo.transform.Translate(new Vector3(-xNeutralVel, 0, 0) * Time.deltaTime);
		}

        if(other.gameObject.tag == Tags.FLOOR && death)
        {
            Destroy(gameObject);
        }
	}

    public void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag == Tags.PLAYER)
        {
            PlayerMechanic player = other.gameObject.GetComponent<PlayerMechanic>();
            
            switch(playerState)
            {
                case PlayerState.ScaryDogMode:
                rigidbody.AddForce(xJumpForce * 2, yJumpForce, 0);
				death = true;
                AudioPlayer.Play(AudioPlayer.PLAYER_WOOF);
                break;

                case PlayerState.BoostMode:
                rigidbody.AddForce(xJumpForce * 2, yJumpForce, 0);
				death = true;
                AudioPlayer.Play(AudioPlayer.CAT_DAMAGED);
                break;

                case PlayerState.Normal:
                player.GameOver();
                break;

                case PlayerState.LaserMode:
				if(!zonzo)
                	player.GameOver();
                break;
            }
        }
    }
}